package com.powergisol.gis.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.powergisol.gis.dao.TabPipeMapper;
import com.powergisol.gis.entity.TabCoordinateSystem;
import com.powergisol.gis.entity.TabPipe;
import com.powergisol.gis.entity.TabPipeStaticColor;
import com.powergisol.gis.util.XyChangeUtil;
import com.powergisol.gis.vo.TabPipeVO;
import com.powergisol.gis.vo.WangXianVO;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Log4j2
public class PipeExcelModelListener extends AnalysisEventListener<TabPipeVO> {
    private String [] tempStr = {"*","x","X"};
    List<TabPipe> tabPipeList = new ArrayList<>();
    private static final int BATCH_COUNT = 4000;
    //测区
    String area = null;
    //图幅号
    String sheetNo = null;
    //管线类型
    String type = null;
    //权属单位
    String unit = null;
    //调查日期
    String time = null;
    WangXianVO wangXianVO;
    TabCoordinateSystem system;
    //临时的管线点预编号
    String pipePreno = null;

    Integer settingId;

    TabPipeMapper tabPipeMapper;

    int total = 0;

    public PipeExcelModelListener() {
    }

    public PipeExcelModelListener(Integer settingId, WangXianVO wangXianVO, TabCoordinateSystem system, TabPipeMapper tabPipeMapper) {
        this.settingId = settingId;
        this.wangXianVO = wangXianVO;
        this.system = system;
        this.tabPipeMapper = tabPipeMapper;
    }

    @Override
    public void invoke(TabPipeVO tabPipeVO, AnalysisContext analysisContext) {
        Integer rowIndex = analysisContext.readRowHolder().getRowIndex();
        if (rowIndex.equals(1)) {
            getIndividual(analysisContext);
        }
        if (rowIndex >= 4) {
            tabPipeVO.setPipeLx(type);
            tabPipeVO.setPipeDrawNo(sheetNo);
            tabPipeVO.setPipeCompany(unit);
            tabPipeVO.setPipeZone(area);
            tabPipeVO.setCheckDate(time);
            TabPipe tabPipe = changeClass(tabPipeVO);
            tabPipeList.add(tabPipe);
        }
        if (tabPipeList.size() >= BATCH_COUNT) {
            saveDate();
            //记录每个sheet所解析出的数据量
            total += tabPipeList.size();
            tabPipeList.clear();
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        //记录每个sheet所解析出的数据量
        total += tabPipeList.size();
        saveDate();
        tabPipeList.clear();
        log.info("管线{}解析完成，总解析{}条数据",type,total);
        //每个sheet结束后清零
        total = 0;
    }

    private void getIndividual(AnalysisContext analysisContext) {
        TabPipeStaticColor color = new TabPipeStaticColor();
        area = analysisContext.readRowHolder().getCellMap().get(0).toString();
        area = area.substring(area.indexOf("：") + 1);
        type = analysisContext.readRowHolder().getCellMap().get(3).toString();
        type = type.substring(type.indexOf("：") + 1);

        sheetNo = analysisContext.readRowHolder().getCellMap().get(6).toString();
        sheetNo = sheetNo.substring(sheetNo.indexOf("：") + 1);

        unit = analysisContext.readRowHolder().getCellMap().get(10).toString();
        unit = unit.substring(unit.indexOf("：") + 1);

        time = analysisContext.readRowHolder().getCellMap().get(15).toString();
        time = time.substring(time.indexOf("：") + 1);
        // System.out.println("解析的第一条数据->\t测区:"+area+"\t类型："+type+"\t图幅号："+sheetNo+"\t权属单位："+unit+"\t调查日期:"+time);
    }

    private TabPipe changeClass(TabPipeVO tabPipeVO) {
        if (StringUtils.isNotBlank(tabPipeVO.getPipePreno())) {
            pipePreno = tabPipeVO.getPipePreno();
        } else {
            tabPipeVO.setPipePreno(pipePreno);
        }
        TabPipe tabPipe = new TabPipe();
        tabPipe.setSettingId(settingId);
        tabPipe.setPipeLx(tabPipeVO.getPipeLx());
        tabPipe.setPipeZone(tabPipeVO.getPipeZone());
        tabPipe.setPipeDrawNo(tabPipeVO.getPipeDrawNo());
        tabPipe.setPipeCompany(tabPipeVO.getPipeCompany());
        tabPipe.setCheckDate(tabPipeVO.getCheckDate());
        tabPipe.setPipePreno(tabPipeVO.getPipePreno());
        tabPipe.setPipeNo(tabPipeVO.getPipeNo());
        tabPipe.setConnectNo(tabPipeVO.getConnectNo());
        tabPipe.setBuryMode(tabPipeVO.getBuryMode());
        tabPipe.setPipeStuff(tabPipeVO.getPipeStuff());
        tabPipe.setPipeSize(tabPipeVO.getPipeSize());
        Boolean flag = false;
        String temp = "";
        for (String s : tempStr) {
            if(tabPipeVO.getPipeSize() != null && tabPipeVO.getPipeSize().contains(s)){
                flag = true;
                temp = s;
            }
        }
        if (flag) {
            String pipeSize = tabPipeVO.getPipeSize();
            Integer pipe1 = Integer.valueOf(pipeSize.substring(0, pipeSize.indexOf(temp)));
            Integer pipe2 = Integer.valueOf(pipeSize.substring(pipeSize.indexOf(temp) + 1));
            if (pipe1 > pipe2) {
                tabPipe.setPipeLong(pipe1.toString());
                tabPipe.setPipeWidth(pipe2.toString());
            } else {
                tabPipe.setPipeLong(pipe2.toString());
                tabPipe.setPipeWidth(pipe1.toString());
            }
        } else {
            tabPipe.setPipeLong(tabPipeVO.getPipeSize());
            tabPipe.setPipeWidth(tabPipeVO.getPipeSize());
        }
        tabPipe.setPipeFeatures(tabPipeVO.getPipeFeatures());
        tabPipe.setPipeAppend(tabPipeVO.getPipeAppend());
        tabPipe.setPipeOx(tabPipeVO.getPipeOx() == null ? null : Double.valueOf(tabPipeVO.getPipeOx()));
        tabPipe.setPipeOy(tabPipeVO.getPipeOy() == null ? null : Double.valueOf(tabPipeVO.getPipeOy()));
        if ("1".equals(wangXianVO.getIsChange())) {
            BigDecimal lat = XyChangeUtil.change(Double.valueOf(tabPipeVO.getPipeOx()), Double.valueOf(tabPipeVO.getPipeOy()), system.getCoorChangeLat());
            BigDecimal lon = XyChangeUtil.change(Double.valueOf(tabPipeVO.getPipeOx()), Double.valueOf(tabPipeVO.getPipeOy()), system.getCoorChangeLon());
            tabPipe.setPipeY(lat);
            tabPipe.setPipeX(lon);
        } else {
            tabPipe.setPipeY(new BigDecimal(tabPipeVO.getPipeOy()));
            tabPipe.setPipeX(new BigDecimal(tabPipeVO.getPipeOx()));
        }
        tabPipe.setPipeGround(tabPipeVO.getPipeGround() == null ? null : Double.valueOf(tabPipeVO.getPipeGround()));
        tabPipe.setPipeTop(tabPipeVO.getPipeTop() == null ? null : Double.valueOf(tabPipeVO.getPipeTop()));
        tabPipe.setPipeBottom(tabPipeVO.getPipeBottom() == null ? null : Double.valueOf(tabPipeVO.getPipeBottom()));
        tabPipe.setPipeDepth(tabPipeVO.getPipeDepth() == null ? null : Double.valueOf(tabPipeVO.getPipeDepth()));
        tabPipe.setPipeNum(tabPipeVO.getPipeNum());
        tabPipe.setPipeRange(tabPipeVO.getPipeRange());
        tabPipe.setPipePower(tabPipeVO.getPipePower());
        tabPipe.setRemark(tabPipeVO.getRemark());
        return tabPipe;
    }

    private void saveDate() {
        tabPipeMapper.saveBatch(tabPipeList);
    }

}
